#sink ( file = "log.txt", type = "output", append = TRUE )

#  set up the raw and managed input directories.
raw_input_dir<- "C:/wspaces/research/genomemap_ws/genomemap_data/raw/"
managed_input_dir<- "C:/wspaces/research/genomemap_ws/genomemap_data/managed/"



#  load genome-wide raw input files

raw_gw_clones_data <- load_raw_gw_clones_data( paste(raw_input_dir,"gw_clones.txt",sep="") );

raw_gw_ncu_data <- load_raw_gw_ncu_data( paste(raw_input_dir,"gw_ncu.txt",sep="") );

raw_gw_gene_ncu_data <- load_raw_gw_gene_ncu_data( paste(raw_input_dir, "gw_gene_ncu.txt", sep="") )

raw_gw_compl_data <- load_raw_compl_data ( paste(raw_input_dir, "gw_compl_data.txt", sep="") )


# load chromosome-wide raw input files

ch_clone_names_data <- list()
ch_probe_names_data <- list()
ch_hybridization_data <- list()
ch_gene_names_data <- list()
ch_genotype_data <- list()

for( linkage_group in 1:7 ){
  
  ch_clone_names_data[[ linkage_group ]] <- load_ch_clone_names_data( 
    paste(raw_input_dir, "clone_names_", linkage_group, ".txt", sep="") )
  
  ch_probe_names_data[[ linkage_group ]] <- load_ch_probe_names_data( 
    paste(raw_input_dir, "probe_names_", linkage_group, ".txt", sep="") )	 
  
  ch_gene_names_data[[ linkage_group ]] <- load_ch_gene_names_data( 
    paste(raw_input_dir, "gene_names_", linkage_group, ".txt", sep="") )
  
  ch_genotype_data[[ linkage_group ]] <- t(load_ch_genotype_data( 
    paste(raw_input_dir, "genotype_data_", linkage_group, ".txt", sep="") ))
  colnames( ch_genotype_data[[ linkage_group ]] ) <- ch_gene_names_data[[ linkage_group ]]
  row.names( ch_genotype_data[[ linkage_group ]] ) <- paste( "Spore-",1:nrow( ch_genotype_data[[ linkage_group ]] ) )
  # missing values
  ch_genotype_data[[ linkage_group ]][ is.na( ch_genotype_data[[ linkage_group ]] ) ] <- 1
  
  ch_hybridization_data[[ linkage_group ]] <- load_ch_hybridization_data( 
    paste(raw_input_dir, "hybridization_data_", linkage_group, ".txt", sep="") )
  row.names( ch_hybridization_data[[ linkage_group ]] ) <- ch_clone_names_data[[ linkage_group ]]
  colnames( ch_hybridization_data[[ linkage_group ]] ) <- ch_probe_names_data[[ linkage_group ]]
  
}

probe_tag_info <- create_probe_tag_info()

# saved until this

# gw integrity checks
check_IC_1()
check_IC_2()
check_IC_3()
check_IC_4()

# chromosome level integrity checks
check_IC_5() 
check_IC_6()
check_IC_7()

# write updated data to managed input directory
write.table( raw_gw_clones_data, 
             file = paste(collapse=managed_input_dir,"gw_clones.txt",sep=""),
             row.names = FALSE,
             col.names = TRUE,
             sep = ",",
             quote = FALSE
)

write.table( raw_gw_ncu_data,
             file = paste(managed_input_dir,"gw_ncu.txt",sep=""),
             row.names = FALSE,
             col.names = TRUE,
             sep = ",",
             quote = FALSE
)

write.table( raw_gw_gene_ncu_data,
             file = paste(managed_input_dir,"gw_gene_ncu.txt",sep=""),
             row.names = FALSE,
             col.names = TRUE,
             sep = ",",
             quote = FALSE
)


for( linkage_group in 1:7 ){
  
  write.table( ch_clone_names_data[[ linkage_group ]],
               file = paste(managed_input_dir, "clone_names_", linkage_group, ".txt", sep=""),
               row.names = FALSE,
               col.names = FALSE,
               quote = FALSE
  )
  
  write.table( ch_probe_names_data[[ linkage_group ]],
               file = paste(managed_input_dir, "probe_names_", linkage_group, ".txt", sep=""),
               row.names = FALSE,
               col.names = FALSE,
               quote = FALSE
  )
  
  write.table( ch_gene_names_data[[ linkage_group ]],
               file = paste(managed_input_dir, "gene_names_", linkage_group, ".txt", sep=""),
               row.names = FALSE,
               col.names = FALSE,
               quote = FALSE
  )
  
  write.table( ch_hybridization_data[[ linkage_group ]],
               file = paste(managed_input_dir, "hybridization_data_", linkage_group, ".txt", sep=""),
               row.names = FALSE,
               col.names = FALSE,
               sep = ",",
               quote = FALSE
  )
  
  write.table( ch_genotype_data[[ linkage_group ]],
               file = paste(managed_input_dir, "genotype_data_", linkage_group, ".txt", sep=""),
               row.names = FALSE,
               col.names = FALSE,
               sep = ",",
               quote = FALSE
  )
  
}

system( "create_data_jar.bat" )



